(function(){"use strict";const b="0.0.9",s={READY:"READY",ELEMENT_CLICKED:"ELEMENT_CLICKED",SELECTION_CLEARED:"SELECTION_CLEARED",SET_STATE:"SET_STATE",UPDATE_TEXT_CONTENT:"UPDATE_TEXT_CONTENT",UPDATE_STYLES:"UPDATE_STYLES",UPDATE_CLASSES:"UPDATE_CLASSES",UPDATE_PROPS:"UPDATE_PROPS",DELETE_ELEMENT:"DELETE_ELEMENT",CLEAR_SELECTION:"CLEAR_SELECTION"},w=["fontSize","fontWeight","fontStyle","textDecoration","lineHeight","textAlign","color","backgroundColor","backgroundImage","margin","padding","borderRadius","boxShadow","opacity"],_=/^(h[1-6]|p|span|button|a|label|li|strong|em|b|i|u)$/;let d=!1,u=!1,o=null,l=null,r=null,m=null,c=null;function D(){l||(l=document.createElement("style"),l.id="sg-visual-editor-styles",l.textContent=` /* Hover state - Pure CSS :hover (no JavaScript needed!) */ /* Only show hover when NOT in saving state and NOT already selected */ body:not([data-sg-saving]) [data-sg-el]:hover:not([data-sg-selected]) { outline: 2px dashed rgba(93,208,220) !important; outline-offset: 2px !important; cursor: pointer !important; transition: outline 0.15s ease !important; } /* Remove hover from parent when child is hovered (only highlight innermost element) */ body:not([data-sg-saving]) [data-sg-el]:has([data-sg-el]:hover):not([data-sg-selected]) { outline: none !important; } /* Selected state - Same dashed outline, clean and minimal */ /* Hide outline when saving */ body:not([data-sg-saving]) [data-sg-selected] { outline: 2px dashed rgba(93,208,220) !important; outline-offset: 2px !important; cursor: pointer !important; transition: outline 0.15s ease !important; } /* Hover tooltip */ /* Hide tooltip when saving */ body:not([data-sg-saving]) .sg-hover-tooltip { position: fixed; background: rgba(93,208,220, 0.95); color: white; padding: 4px 8px; border-radius: 4px; font-size: 12px; font-weight: 600; font-family: monospace; text-transform: uppercase; pointer-events: none; z-index: 999999; transition: opacity 0.15s ease; box-shadow: 0 2px 8px rgba(0,0,0,0.15); letter-spacing: 0.5px; } `,document.head.appendChild(l))}function I(){l&&(l.remove(),l=null),O()}function P(){return r||(r=document.createElement("div"),r.className="sg-hover-tooltip",r.style.display="none",document.body.appendChild(r),r)}function O(){r&&(r.remove(),r=null),m=null}function R(e){r||P();const t=e.tagName.toLowerCase();r.textContent=`<${t}>`,r.style.display="block";const n=e.getBoundingClientRect(),i=n.left,a=n.top-24;r.style.left=i+"px",r.style.top=a+"px"}function v(){r&&(r.style.display="none"),m=null}function E(e,t){try{window.parent.postMessage({type:e,payload:t,source:"softgen-iframe"},"*")}catch(n){console.error("[Visual Editor] postMessage failed:",n)}}function k(e){const t=e.getAttribute("data-sg-el");if(!t)return null;const n=t.match(/^(.+):(\d+):(\d+)$/);return n?{id:t,filePath:n[1],line:parseInt(n[2],10),column:parseInt(n[3],10)}:null}function y(e){const t=e.tagName.toLowerCase();if(t==="code"||t==="pre")return!0;const n=window.getComputedStyle(e).whiteSpace;return n==="pre"||n==="pre-line"||n==="pre-wrap"}function h(e){const t=e.tagName.toLowerCase();return y(e)?{isTextEditable:!1,canEditStyles:!1}:_.test(t)?{isTextEditable:!0,canEditStyles:!0}:{isTextEditable:V(e),canEditStyles:!0}}function V(e){let t=!1,n=!1;for(const i of e.childNodes)i.nodeType===Node.TEXT_NODE&&i.textContent.trim()?t=!0:i.nodeType===Node.ELEMENT_NODE&&(n=!0);return t&&!n}function M(e){return y(e)?e.textContent||"":e.innerText?.trim()||""}function f(e){e&&(e.removeAttribute("data-sg-selected"),v())}function g(e){let t=e;for(;t&&t!==document.body;){if(t.hasAttribute?.("data-sg-el"))return t;t=t.parentElement}return null}function H(e,t){const n=k(e),i=h(e),a=e.tagName.toLowerCase(),$=typeof e.className=="string"?e.className:e.className?.baseVal||"",T={componentPath:n?.filePath||"",line:n?.line||0,column:n?.column||0,componentName:e.getAttribute("data-sg-name")||a,element:{tagName:a,textContent:M(e),className:$,...a==="img"?{src:e.getAttribute("src")||""}:{}},isTextEditable:i.isTextEditable,canEditStyles:i.canEditStyles};if(t){const G=window.getComputedStyle(e);T.styles={},w.forEach(x=>{T.styles[x]=G[x]})}return T}function S(e){if(!d)return;const t=g(e.target);if(e.preventDefault(),e.stopPropagation(),o&&f(o),t){const n=h(t);if(!n.isTextEditable&&!n.canEditStyles)return;o=t,t.setAttribute("data-sg-selected","true"),E(s.ELEMENT_CLICKED,H(t,!0))}else o=null,E(s.ELEMENT_CLICKED,null)}function L(e){d&&e.key==="Escape"&&(o&&(f(o),o=null),E(s.SELECTION_CLEARED,{}))}function p(e){if(!d)return;const t=i=>{if(!i)return!1;const a=i.tagName?.toLowerCase();return a==="a"||a==="button"||e.type==="submit"},n=g(e.target);t(n||e.target)&&(e.preventDefault(),e.stopPropagation())}function C(e){if(u)return;const t=g(e.target);if(!t||t===o){t||v();return}m=t,R(t)}function N(e){if(u)return;g(e.target)===m&&v()}function U(){document.addEventListener("submit",p,!0),document.addEventListener("click",p,!0),document.addEventListener("click",S,!0),document.addEventListener("keydown",L,!0),document.addEventListener("mouseover",C,!0),document.addEventListener("mouseout",N,!0)}function Y(){document.removeEventListener("submit",p,!0),document.removeEventListener("click",p,!0),document.removeEventListener("click",S,!0),document.removeEventListener("keydown",L,!0),document.removeEventListener("mouseover",C,!0),document.removeEventListener("mouseout",N,!0)}function j(){console.debug("[Visual Editor] Enabling edit mode"),D(),U(),document.body.style.cursor="crosshair";const e=document.createElement("style");e.id="sg-scroll-override",e.textContent="* { scroll-behavior: auto !important; }",document.head.appendChild(e)}function z(){console.debug("[Visual Editor] Disabling edit mode"),Y(),document.body.style.cursor="",document.body.removeAttribute("data-sg-saving"),o&&f(o),o=null,I(),document.getElementById("sg-scroll-override")?.remove()}const K={[s.SET_STATE]:e=>{const t=e?.state;if(!["disabled","enabled","saving"].includes(t)){console.error("[Visual Editor] Invalid state:",t);return}console.debug(`[Visual Editor] Command: SET_STATE \u2192 ${t}`),c&&(clearInterval(c),c=null,console.debug("[Visual Editor] Platform connected, stopped READY retries")),t==="enabled"?(d||(j(),d=!0),u&&(document.body.removeAttribute("data-sg-saving"),u=!1)):t==="disabled"?(z(),d=!1,u=!1):t==="saving"&&(document.body.setAttribute("data-sg-saving","true"),u=!0,o&&(f(o),o=null))},[s.UPDATE_TEXT_CONTENT]:e=>{if(!o){console.warn("[Visual Editor] No element selected for text update");return}e?.text!==void 0&&(o.textContent=e.text)},[s.UPDATE_STYLES]:e=>{if(!o){console.warn("[Visual Editor] No element selected for style update");return}if(!e||typeof e!="object"){console.error("[Visual Editor] Invalid styles payload:",e);return}Object.keys(e).forEach(t=>{e[t]!==void 0&&e[t]!==null&&(o.style[t]=e[t])})},[s.UPDATE_CLASSES]:e=>{if(!o){console.warn("[Visual Editor] No element selected for class update");return}if(!e||typeof e!="object"){console.error("[Visual Editor] Invalid classes payload:",e);return}e.remove&&Array.isArray(e.remove)&&e.remove.forEach(t=>{t&&typeof t=="string"&&o.classList.remove(t)}),e.add&&Array.isArray(e.add)&&e.add.forEach(t=>{t&&typeof t=="string"&&o.classList.add(t)})},[s.UPDATE_PROPS]:e=>{if(!o){console.warn("[Visual Editor] No element selected for props update");return}if(!e||typeof e!="object"){console.error("[Visual Editor] Invalid props payload:",e);return}Object.entries(e).forEach(([t,n])=>{n!=null&&(n===""?o.removeAttribute(t):o.setAttribute(t,n))})},[s.DELETE_ELEMENT]:()=>{o&&o.parentNode&&(o.parentNode.removeChild(o),o=null)},[s.CLEAR_SELECTION]:()=>{o&&(f(o),o=null),E(s.SELECTION_CLEARED,{})}};window.addEventListener("message",e=>{if(!e.data||typeof e.data!="object"||e.data.source!=="softgen-editor"||e.source!==window.parent)return;const{type:t,payload:n}=e.data,i=K[t];i?i(n):console.warn("[Visual Editor] Unknown message type:",t)});function A(e){E(s.READY,{version:b,url:window.location.href,timestamp:new Date().toISOString(),taggedElementsCount:e})}function X(){const e=()=>{const t=document.querySelectorAll("[data-sg-el]");if(t.length===0){console.debug("[Visual Editor] No tagged elements found on page. Editor will not initialize.");return}const n=t.length;A(n);let i=0;const a=15;c=setInterval(()=>{if(i++,i>=a){clearInterval(c),c=null,console.warn("[Visual Editor] Platform not responding after 15s. Giving up retry.");return}A(n)},1e3)};document.readyState==="loading"?document.addEventListener("DOMContentLoaded",e):e()}X(),console.debug(`[Softgen Visual Editor] v${b} initialized`)})(); //# sourceMappingURL=visual-editor.min.js.map